<template>
  <van-icon
    :class="{liked:value===1}"
    :loading="loading"
    :name=" value ===1 ? 'good-job' : 'good-job-o'"
    @click="onCollect"
  />
</template>

<script>
import { addlikings, deletelikings } from '@/api/article'

export default {
  name: 'ArticleLinking',
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onCollect () {
      this.loading = true
      try {
        let status = -1
        if (this.value === 1) {
          await deletelikings(this.articleId)
        } else {
          await addlikings(this.articleId)
          status = 1
        }
        // 更新视图
        this.$emit('input', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')

      } catch (err) {
        this.$toast.fail('操作失败')
      }


      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.liked {
  color: #e5645f;
}
</style>